<?php
declare(strict_types=1);

namespace Dao;
// hook dao_store_product_use.php

use Field\StoreProductConst;
use Model\StoreProductModel;
use Swoole\Timer;

class StoreProductDao extends StoreProductModel
{
    // hook dao_store_product_public_start.php

    public $key_id = 'Product:I:';
    public $key_cate = 'Product:C:';
    public $key_pinyin = 'Product:P:';

    protected array $add_rule = [];
    protected array $edit_rule = [];

    protected array $rule = [
        /*
        "mobile"=>[
            "max"=>3
            "min"=>3
            "func"=>'isMobile'
            "error"=>false,
            'message'=>'格式有误'
        ],
        "username"=>[
            "max"=>3
            "min"=>3
            "func"=>'checkS'
            "error"=>false,
            'message'=>'格式有误'
        ],
        */
    ];
    //数据，字段，验证规则
    /*
    function isMobile($data,$field,$val){

        return true;
    }
    */

    // hook dao_store_product_public_end.php


    public function auto_load()
    {
        // hook model_user_auto_load_start.php
        if ($this->http_server->worker_id == 0) {
            Timer::after(5000, function () {
                $this->reload();
            });
        }
    }

    public function reload($cond = [StoreProductConst::Status=>1])
    {
        $data = $this->select($cond);
        $content = $this->StoreProductContent->select();
        $products = arrlist_group($content, 'ProductID');
        $QiNiuHost = _CONF('OssHost');
        $AttachIDS = [];
        $Cate=[];
        if (!empty($data)) {
            foreach ($data as $k=> $row) {
                //AttachID VideoAttachID AttachIDArr
                empty($Cate[$row[StoreProductConst::CateID]]) &&  $Cate[$row[StoreProductConst::CateID]]=[];
                $Cate[$row[StoreProductConst::CateID]][] = $row[StoreProductConst::ProductID];
                $data[$k][StoreProductConst::AttachIDArr] = explode(',', $row[StoreProductConst::AttachIDArr] ?? '');
                !empty($row[StoreProductConst::AttachID]) && $AttachIDS[] = $row[StoreProductConst::AttachID];
                !empty($row[StoreProductConst::VideoAttachID]) && $AttachIDS[] = $row[StoreProductConst::VideoAttachID];
                !empty($row[StoreProductConst::AttachIDArr] ) && array_push($AttachIDS,...explode(',', $row[StoreProductConst::AttachIDArr]));
                //AttachID
                $this->CacheSet($this->key_pinyin . $row[StoreProductConst::Pinyin], $row[StoreProductConst::ProductID]);
                $data[$k]['Content'] = $products[$row[StoreProductConst::ProductID]] ?? [];
                foreach ($data[$k]['Content'] as $v) {
                  !empty($v['AttachID']) && $AttachIDS[] = $v['AttachID'];
                }
            }
            $AttachIDS = array_filter(array_unique($AttachIDS));
            if($AttachIDS){
                $AttachS = !empty($AttachIDS) ? arrlist_key_values($this->SysAlbumAttach->select(['AttachID' => $AttachIDS],[],0,0,'AttachID,Patch'), 'AttachID','Patch') : [];
                foreach ($data as &$row2) {
                    $row2['Attach'] = !empty($AttachS[$row2[StoreProductConst::AttachID]]) ? $QiNiuHost.'/'.$AttachS[$row2[StoreProductConst::AttachID]] : '';
                    $row2['VideoAttach'] = !empty($AttachS[$row2[StoreProductConst::VideoAttachID]]) ? $QiNiuHost.'/'.$AttachS[$row2[StoreProductConst::VideoAttachID]] : '';
                    if(!empty($row2[StoreProductConst::AttachIDArr])){
                        $row2['AttachArray']=[];
                        foreach ($row2[StoreProductConst::AttachIDArr] as $Id){
                         !empty($AttachS[$Id]) &&   $row2['AttachArray'][] = $QiNiuHost.'/'.$AttachS[$Id];
                        }
                    }
                    if(!empty($row2['Content'])){
                        foreach ($row2['Content'] as  &$v) {
                            $v['Attach'] = !empty($AttachS[$row2[StoreProductConst::AttachID]]) ? $QiNiuHost.'/'.$AttachS[$row2[StoreProductConst::AttachID]] : '';
                        }
                        $row2['Content'] = arrlist_sort_by_many_field($row2['Content'],'Rank',SORT_DESC);
                        $row2['Content'] = arrlist_group($row2['Content'],'TypeID');
                    }
                }
            }
            foreach ($data as $row) {
                $this->CacheSet($this->key_id . $row[StoreProductConst::ProductID], $row);
            }
        }

        foreach ($Cate as $k=> $ID){
            $this->CacheSet($this->key_cate . $k , implode(',',$ID));
        }
    }
    // hook dao_store_product_end.php
}

?>